package com.yxc.elasticsearchdemo.config;

import org.elasticsearch.common.Nullable;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.mapping.context.MappingContext;

public class CustomMappingEsConverter extends MappingElasticsearchConverter {

    public CustomMappingEsConverter(MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext, GenericConversionService conversionService) {
        super(mappingContext, conversionService);
    }

    public CustomMappingEsConverter(
            MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
        this(mappingContext, null);
    }

    @Override
    public Document mapObject(@Nullable Object source) {
        Document target = Document.create();
        if (source != null) {
            this.write(source, target);
        }
        target.remove("_class"); // 继承MappingEsConverter类，并重写mapObject类，去掉_class字段
        return target;
    }
}